<?
/*
 *      inventory.class.php
 *      
 *      Copyright 2009 Valery Tverdohleb <tvsdesign@gmail.com>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

class Inventory
{
	protected $id;
	
	public function __construct($id)
	{
		$this->id = $id;
	}

	public function dropItem($itemid)
	{
		$stmt = Engine::get()->db->prepare('UPDATE `items` AS i, `users` AS u SET i.`X` = u.`X`, i.`Y` = u.`Y`, i.`Z` = u.`Z`, i.`L` = u.`L`, i.`owner` = 0 WHERE i.`owner` = u.`id` AND u.`id` = :uid AND i.`id` = :iid;');
		$stmt->bindValue(':uid', $this->id);
		$stmt->bindValue(':iid',$itemid);
		$stmt->execute();
	}
	
	public function listItemsOnGround($coords)
	{
		$stmt = Engine::get()->db->prepare('SELECT i.`id`, t.`title` FROM `items` AS i LEFT JOIN `item_types` AS t ON t.`id` = i.`itype` WHERE `X`=:X AND `Y`=:Y AND `Z`=:Z AND `L`=:L AND `owner` = 0;');
		$stmt->bindValue(':X',$coords['X']);
		$stmt->bindValue(':Y',$coords['Y']);
		$stmt->bindValue(':Z',$coords['Z']);
		$stmt->bindValue(':L',$coords['L']);
		$stmt->execute();
		return $stmt->fetchAll(PDO::FETCH_ASSOC);
	}
	
	public function takeItem($itemid)
	{
		$stmt = Engine::get()->db->prepare('UPDATE `items` SET `owner` = :uid WHERE `id` = :iid;');
		$stmt->bindValue(':uid', $this->id);
		$stmt->bindValue(':iid',$itemid);
		$stmt->execute();
	}
	
	public function listItems()
	{
		$stmt = Engine::get()->db->prepare('SELECT i.`id`, t.`title`, t.`description`, t.`type`, t.`wearing` FROM `items` AS i LEFT JOIN `item_types` AS t ON t.`id` = i.`itype` WHERE i.`owner` = :id AND i.`id` NOT IN (SELECT `itemid` FROM `equipment`);');
		$stmt->bindValue('id',$this->id);
		$stmt->execute();
		return $stmt->fetchAll(PDO::FETCH_ASSOC);
	}
	
	public function isItemInBag($itemid)
	{
		$stmt = Engine::get()->db->prepare('SELECT COUNT(*) FROM `items` WHERE `id` = :itemid AND `owner` = :id;');
		$stmt->bindValue('itemid',$itemid);
		$stmt->bindValue('id',$this->id);
		$stmt->execute();
		return $stmt->fetchColumn() != 0;
	}
	
	public function wearItem($itemid)
	{
		$stmt = Engine::get()->db->prepare('INSERT INTO `equipment` (`userid`, `slot`, `itemid`) SELECT i.`owner`, t.`wearing`, i.`id` FROM `items` AS i LEFT JOIN `item_types` AS t ON t.`id` = i.`itype` WHERE i.`id` = :itemid ON DUPLICATE KEY UPDATE `itemid` = :itemid;');
		$stmt->bindValue(':itemid',$itemid);
		$stmt->execute();
	}
	
	public function takeOffItem($slot)
	{
		$stmt = Engine::get()->db->prepare('DELETE FROM `equipment` WHERE `userid` = :id AND `slot` = :slot LIMIT 1;');
		$stmt->bindValue(':id', $this->id);
		$stmt->bindValue(':slot', $slot);
		$stmt->execute();
	}
	
	public function getWearedItems()
	{
		$stmt = Engine::get()->db->prepare('SELECT e.`slot`, e.`itemid` AS id, t.`title` FROM `equipment` AS e LEFT JOIN `items` AS i ON i.`id` = e.`itemid` LEFT JOIN `item_types` AS t ON t.`id` = i.`itype` WHERE e.`userid` = :id;');
		$stmt->bindValue(':id', $this->id);
		$stmt->execute();
		$items_raw = $stmt->fetchAll(PDO::FETCH_ASSOC);
		$items = array();
		foreach ($items_raw as $i)
		{
			if ($i['id'] != 0)
			{
				$items[$i['slot']] = $i;
			}
		}
		return $items;
	}
	
}

?>
